<template>
    <div class="achievement-list-page">
        <el-card shadow="never" class="mt-4">
            <el-form :inline="true" @keyup.enter.native="onSubmit">
                <el-form-item label="类型" prop="type">
                    <el-select
                        v-model="form.type"
                        filterable
                        clearable
                        @change="onSubmit()">
                        <el-option
                            v-for="item in typeOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="关键词" prop="name">
                    <el-input
                    v-model="form.name"
                    placeholder="搜索学习路径（岗位角色/学习计划/专题）" class="input-width"></el-input>
                </el-form-item>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
                <el-button type="primary" @click="onResetForm">重置</el-button>
            </el-form>
            <div class="mt-3" v-loading="loading">
                <el-table :data="data">
                    <el-table-column
                    type="index"
                    :index="indexMethod"
                    align="center"
                    label="序号"
                    width="50"/>
                    <el-table-column label="名称" prop="name"></el-table-column>
                    <el-table-column label="图片">
                        <template slot-scope="{row}">
                            <img :src="row.img" class="logo" v-if="row.img"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="类型" width="250">
                        <template slot-scope="{row}">
                            <el-tag v-for="(v, i) in transType(row.type)" :key="i" class="m-1">
                                {{ v }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="视频/文章数" prop="count"/>
                    <el-table-column label="所属企业" prop="company"/>
                    <el-table-column label="操作">
                        <template slot-scope="{row}">
                            <el-button type="text" @click="onEditClick(row)">编辑</el-button>
                            <el-button type="text" @click="onPublishClick(row)">
                                {{ row.status ? "下架" : "上架" }}</el-button>
                            <el-button
                                type="text"
                                @click="onDeleteClick(row)"
                                class="red-color">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div v-if="totalElements > 0" class="footer">
                <pagination
                ref="pagination"
                :currentPage="form.page"
                :pageSize="form.size"
                :handleCurrentChange="onCurrentChange"
                :handlePageSizeChange="onPageSizeChange"
                :totalElements="totalElements"
                ></pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import Pagination from '../../components/base/Pagination';
import IndexNumber from '../../helpers/IndexNumber';

export default {
    components: {
        Pagination,
    },
    mixins: [require('../../mixins/CacheQuery').default],
    data() {
        return {
            loading: false,
            data: [],
            form: {
                page: 1,
                size: 50,
                name: '',
                type: '',
                filter: 'company',
            },
            totalElements: 0,
            typeOptions: [
                {id: 1, name: '岗位角色'},
                {id: 2, name: '学习计划'},
                {id: 3, name: '专题'},
            ],
        };
    },
    filters: {
        typeFilter: function (value) {
            if (!value) return '';
            const options = ['岗位角色', '学习计划', '专题'];
            const typeList = JSON.parse(value);
            const types = typeList.map(type => options[(type - 1)]);
            return types.join(', ');
        },
    },
    methods: {
        isCompanyAccount() {
            return this.$store.state.my.company_id > 0;
        },
        onSubmit() {
            this.form.page = 1;
            this.fetchData();
        },
        onResetFilters() {
            this.$refs.form.resetFields();
        },
        onCurrentChange(val) {
            this.form.page = val;
            this.fetchData();
        },
        onPageSizeChange(val) {
            this.form.size = val;
            this.form.page = 1;
            this.fetchData();
        },
        fetchData() {
            this.loading = true;
            API.get('label', {params: this.form}).then((res) => {
                this.data = res.content;
                this.totalElements = res.pagination.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        onEditClick({id}) {
            this.$router.push({
                name: 'CompanyLabelManage.Add',
                params: { id: id },
            });
        },
        onDeleteClick(row) {
            if (row.status) {
                $ele.$message.error('先对标签下架后才进行删除操作');
                return;
            }
            $ele.$confirm('是否删除此标签', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.loading = true;
                API.delete(`label/${row.id}`, {
                }).then(() => {
                    $ele.$message.success('删除成功');
                    this.fetchData();
                }).finally(() => {
                    this.loading = false;
                });
            }).catch(() => { });
        },
        onPublishClick(row) {
            const content = row.status ? '是否下架标签' : '是否上架标签';
            const requestStatus = !row.status;
            $ele.$confirm(content, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.loading = true;
                API.put(`label/${row.id}/${requestStatus}`).then(() => {
                    $ele.$message.success('操作成功');
                    this.fetchData();
                }).finally(() => {
                    this.loading = false;
                });
            }).catch(() => { });
        },
        transType(value) {
            if (!value) return [];
            const options = ['岗位角色', '学习计划', '专题'];
            const typeList = JSON.parse(value);
            const types = typeList.map(type => options[(type - 1)]);
            return types;
        },
        onResetForm() {
            this.form.type = '';
            this.form.name = '';
            this.fetchData();
        },
        indexMethod(index) {
            return new IndexNumber()
                .getIndex(this.totalElements, this.form.page, this.form.size, index);
        },
    },
    mounted() {
        this.fetchData();
    },
};
</script>

<style lang="scss">
.achievement-list-page {
    .filter {
        .input-width {
            max-width: 200px;
        }
    }
    .logo {
        width: 60px;
        height: 60px;
    }
    .footer {
        height: 44px;
        background: #f7f7f7;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding-left: 20px;
        padding-right: 20px;
    }
    .image-slot {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
        background: #f5f7fa;
        color: #909399;
    }
}
</style>
